{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4abf3d40",
   "metadata": {},
   "source": [
    "## 1.Python 使用API模板"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "968ab2a0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "dcaa93f2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备API的接口身份\n",
    "API_KEY = \"5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4\"\n",
    "API_Secret =\"rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi\"\n",
    "\n",
    "# 3.准备base URL（face detect）\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload\n",
    "payload = {\n",
    "    \"api_key\" : API_KEY,\n",
    "    \"api_secret\" : API_Secret,\n",
    "    \"image_url\" : \"https://img2.baidu.com/it/u=586072515,1919239108&fm=253&fmt=auto&app=120&f=JPEG?w=519&h=500\"\n",
    "}\n",
    "\n",
    "# 6.请求数据\n",
    "r = requests.post(url = base_url,params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "c18452d3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "566a0300",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648279730,38e22b38-f488-41a3-b96d-53e3a078251f\",\"time_used\":132,\"faces\":[{\"face_token\":\"e92d52c8c7ed697cd22a66ae96692e51\",\"face_rectangle\":{\"top\":146,\"left\":165,\"width\":206,\"height\":206}}],\"image_id\":\"BDe3FTWIopLlhKeLOv4qFg==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "813ab6c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e92d52c8c7ed697cd22a66ae96692e51'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "bdfd2816",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'e92d52c8c7ed697cd22a66ae96692e51'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0efa57de",
   "metadata": {},
   "source": [
    "## 2.人脸分析API\n",
    "* 请同学们使用上述模板合理调用参数来实现人脸分析API\n",
    "* 要求：获取 return_attributes的所有参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "38cea22a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "analyze_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4'\n",
    "API_sercret = 'rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_tokens':face_token,\n",
    "    'return_attributes':'gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = analyze_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "ae28b666",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e9927af6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 166,\n",
       " 'request_id': '1648279737,ef95fa66-707e-424e-a408-233075b62d87',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.04,\n",
       "     'neutral': 99.883,\n",
       "     'disgust': 0.004,\n",
       "     'anger': 0.004,\n",
       "     'surprise': 0.027,\n",
       "     'fear': 0.012,\n",
       "     'happiness': 0.029},\n",
       "    'beauty': {'female_score': 84.355, 'male_score': 80.387},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 32},\n",
       "    'mouthstatus': {'close': 99.997,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.003,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 2.683,\n",
       "     'stain': 2.981,\n",
       "     'acne': 20.304,\n",
       "     'health': 55.561},\n",
       "    'headpose': {'yaw_angle': 4.070139,\n",
       "     'pitch_angle': 6.834269,\n",
       "     'roll_angle': -4.4713683},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 1.114},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 1.114},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 1.114}},\n",
       "    'smile': {'threshold': 50.0, 'value': 0.001},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.001,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.001,\n",
       "      'no_glass_eye_open': 99.998,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 90.078},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.514,\n",
       "      'vector_z_component': 0.99,\n",
       "      'vector_x_component': -0.012,\n",
       "      'vector_y_component': -0.138,\n",
       "      'position_y_coordinate': 0.43},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.463,\n",
       "      'vector_z_component': 0.968,\n",
       "      'vector_x_component': -0.057,\n",
       "      'vector_y_component': -0.243,\n",
       "      'position_y_coordinate': 0.422}}},\n",
       "   'face_rectangle': {'width': 206, 'top': 146, 'left': 165, 'height': 206},\n",
       "   'face_token': 'e92d52c8c7ed697cd22a66ae96692e51'}]}"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "3ffdb79e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8fa18efc",
   "metadata": {},
   "source": [
    "### 1. 数据处理小魔法 pandas.json_normoalize() 实现数据表格化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "a4ffd64a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>e92d52c8c7ed697cd22a66ae96692e51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.04</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>99.883</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.004</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.027</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>0.029</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>84.355</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>80.387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>32</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.003</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>2.683</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>2.981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>20.304</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>55.561</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>4.070139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>6.834269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>-4.471368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>1.114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>1.114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>1.114</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.998</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>90.078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.138</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.43</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.463</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.968</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.243</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>206</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          e92d52c8c7ed697cd22a66ae96692e51\n",
       "attributes.emotion.sadness                                                      0.04\n",
       "attributes.emotion.neutral                                                    99.883\n",
       "attributes.emotion.disgust                                                     0.004\n",
       "attributes.emotion.anger                                                       0.004\n",
       "attributes.emotion.surprise                                                    0.027\n",
       "attributes.emotion.fear                                                        0.012\n",
       "attributes.emotion.happiness                                                   0.029\n",
       "attributes.beauty.female_score                                                84.355\n",
       "attributes.beauty.male_score                                                  80.387\n",
       "attributes.gender.value                                                       Female\n",
       "attributes.age.value                                                              32\n",
       "attributes.mouthstatus.close                                                  99.997\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                    0.003\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              2.683\n",
       "attributes.skinstatus.stain                                                    2.981\n",
       "attributes.skinstatus.acne                                                    20.304\n",
       "attributes.skinstatus.health                                                  55.561\n",
       "attributes.headpose.yaw_angle                                               4.070139\n",
       "attributes.headpose.pitch_angle                                             6.834269\n",
       "attributes.headpose.roll_angle                                             -4.471368\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 1.114\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               1.114\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             1.114\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                         0.001\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                             100.0\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                             0.001\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                0.001\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                            99.998\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  90.078\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.514\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                              0.99\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                            -0.012\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.138\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                              0.43\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.463\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.968\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                            -0.057\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.243\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.422\n",
       "face_rectangle.width                                                             206\n",
       "face_rectangle.top                                                               146\n",
       "face_rectangle.left                                                              165\n",
       "face_rectangle.height                                                            206"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "904dab13",
   "metadata": {},
   "source": [
    "## 3. 人脸对比\n",
    "* 思考：人脸对比至少需要几张脸？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52638b72",
   "metadata": {},
   "source": [
    "### 1.人脸检测获取token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "31b2a8f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4'\n",
    "API_sercret = 'rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img2.baidu.com/it/u=586072515,1919239108&fm=253&fmt=auto&app=120&f=JPEG?w=519&h=500'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "9f940ad6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1545d6138cb75e89e217b8ed8ad7c0b1'"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "ae407da5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4'\n",
    "API_sercret = 'rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'image_url':'https://img2.baidu.com/it/u=3039683039,649732959&fm=253&fmt=auto&app=138&f=JPEG?w=281&h=500'\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "9f45497f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0d90019da4ea7b775966cf65cf7527b5'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0]['face_token']\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b299edf",
   "metadata": {},
   "source": [
    "### 2. 人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "1918ac7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = '5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4'\n",
    "API_sercret = 'rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi'\n",
    "\n",
    "# 3. 请求方法：POST\n",
    "\n",
    "# 4. 准备payload（酬载）\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_sercret,\n",
    "    'face_token1':face_token01,\n",
    "    'face_token2':face_token02\n",
    "}\n",
    "\n",
    "# 5. 发起请求\n",
    "r = requests.post(url = compare_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "b23a5eb9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "ebb26240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648280112,9006b505-689c-40a3-8403-fe9cb473ec00',\n",
       " 'time_used': 437,\n",
       " 'confidence': 30.525,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9d616a1a",
   "metadata": {},
   "source": [
    "## 4.封装人脸检测函数，实现快速调用\n",
    "* 准备工作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "5fb50b32",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = '5G6CNIUGghmy_JcyAxNV-bDSVWy0XeI4'\n",
    "API_sercret = 'rdArZ6BfSA19vSCeC_ArYzk2jqXK00Mi'\n",
    "img_url_01 = 'https://img2.baidu.com/it/u=586072515,1919239108&fm=253&fmt=auto&app=120&f=JPEG?w=519&h=500'\n",
    "img_url_02 = 'https://img2.baidu.com/it/u=3039683039,649732959&fm=253&fmt=auto&app=138&f=JPEG?w=281&h=500'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "c066bfcf",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_sercret,img_url):\n",
    "    \"\"\"face++ API人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "1e1431d2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648281155,978b1ee3-693e-4d3c-9ce8-69c5b58b11e5',\n",
       " 'time_used': 145,\n",
       " 'faces': [{'face_token': '48e1d67ba703ebf0cdb175078c8404b0',\n",
       "   'face_rectangle': {'top': 146, 'left': 165, 'width': 206, 'height': 206}}],\n",
       " 'image_id': 'BDe3FTWIopLlhKeLOv4qFg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "300068ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648281160,786e8f3c-39b0-4196-8e68-58d6f3f3a6f7',\n",
       " 'time_used': 64,\n",
       " 'faces': [{'face_token': 'b0703e7da002089bf8606d593b92f180',\n",
       "   'face_rectangle': {'top': 143, 'left': 104, 'width': 88, 'height': 88}}],\n",
       " 'image_id': 'UO9tzrlC1/A9FP4mtlzrww==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd899d43",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
